home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1989 / 03 / examroom.lst < prev    next >
File List  |  1989-02-10  |  11KB  |  363 lines

  1. _EXAMINING ROOM - THE PORTABILITIY DREAM_
  2. by Margaret Johnson
  3.  
  4.  
  5. [LISTING ONE]
  6.  
  7. /**********************************************************************
  8.  XVT "Hello World"
  9.  **********************************************************************/
  10. #include "xvt.h"                     /* standard XVT header */
  11. #include "xvtmenu.h"                 /* standard XVT menu tags */
  12.  
  13.  
  14. /*
  15.        Required application setup structure.
  16. */
  17. APPL_SETUP appl_setup = {              
  18.         0,                /* menu bar resource ID (use default) */
  19.         0,                /* about box resource ID (use default) */
  20.         "Hello World!",   /* application's name */
  21.         W_DOC,            /* type of initial window */
  22.         TRUE,             /* size box on initial window? */
  23.         FALSE,            /* vert. scroll bar on initial window? */
  24.         FALSE,            /* horz. scroll bar on initial window? */
  25.         TRUE,             /* close box on initial window? */
  26.         FALSE,            /* want std. font menu? (includes sizes) */
  27.         FALSE             /* want std. style menu? */
  28. };
  29. /*********************************************************************
  30.  *       Main application entry point.
  31.  *********************************************************************/
  32. void main_event(win, ep)
  33. WINDOW win;
  34. EVENT *ep;
  35.  
  36. { RCT  rct;
  37.  
  38.   switch(ep->type)
  39.    {
  40.     case E_UPDATE:
  41.         get_client_rect(win,&rct);
  42.         set_pen(&white_pen);
  43.         draw_rect(&rct);
  44.         draw_text(10,100, "Hello World!", -1);
  45.         break;
  46.     case E_COMMAND:
  47.         if (ep->v.cmd.tag == M_FILE_QUIT)
  48.            terminate();
  49.         break;
  50.     case E_CLOSE:
  51.         terminate();
  52.         break;
  53.     case E_QUIT:
  54.         if (ep->v.query)
  55.               quit_OK();
  56.         else
  57.               terminate();
  58.         break;
  59.     }
  60. }
  61.  
  62. /***********************************************************************
  63.  *      Application cleanup.  Nothing to do.
  64.  ***********************************************************************/
  65. void appl_cleanup()
  66. {
  67. }
  68. /**********************************************************************
  69.  *       Application initialization.
  70.  **********************************************************************/
  71. BOOLEAN appl_init()
  72. {
  73.  return(TRUE);
  74. }
  75.  
  76. [LISTING TWO]
  77.  
  78. /***********************************************************************
  79.  * Mac "Hello World"
  80.  ***********************************************************************/
  81. #include <QuickDraw.h>
  82. #include <WindowMgr.h>
  83. #include <ControlMgr.h>
  84. #include <EventMgr.h>
  85. #include <DeskMgr.h>
  86. #include <MenuMgr.h>
  87.  
  88. GrafPtr       w_port;
  89. Rect          drag_rect, grow_bounds;
  90. WindowRecord  w_record;      /* storage for a window's information */
  91. WindowPtr     hello_window; /* a pointer to that storage */
  92.  
  93. #define  mk_long(x)  (*((long *)&(x)))
  94.  
  95. main()
  96. {
  97.  init_process();            /* do all the initialization */
  98.  make_window();
  99.  event_loop();
  100. }
  101.  
  102. /**********************************************************************/
  103.  init_process()
  104.  {
  105.   init_mgrs();
  106.   set_parameters();
  107.  }
  108.  
  109. /**********************************************************************/
  110. init_mgrs()
  111. {
  112.  InitGraf(&thePort);
  113.  InitFonts();
  114.  FlushEvents(everyEvent,0);
  115.  InitWindows();
  116.  InitCursor();
  117. /**********************************************************************/
  118. set_parameters()
  119. {
  120.  drag_rect = thePort->portRect;
  121.  SetRect(&grow_bounds, 64, 64, thePort->portRect.right, 
  122.          thePort->portRect.bottom);
  123. }
  124. /**********************************************************************/
  125. make_window()
  126. {
  127.  hello_window = GetNewWindow(128,&w_record,-1L);  
  128. }
  129. /**********************************************************************/
  130. event_loop()
  131. {
  132.  EventRecord event;
  133.  
  134.  while (1)
  135.    {SystemTask();
  136.     GetNextEvent(everyEvent, &event);
  137.     switch(event.what)
  138.        {
  139.         case mouseDown:
  140.             do_mouse_down(&event);
  141.             break;
  142.             
  143.         case updateEvt:
  144.             do_update(&event);
  145.             break;
  146.             
  147.         case activateEvt:
  148.             do_activate(&event);
  149.             break;
  150.             
  151.         default:
  152.             break;
  153.        }
  154.    }
  155. }
  156. /**********************************************************************/
  157. do_mouse_down(eventp)
  158.    EventRecord *eventp;
  159.  {
  160.    WindowPtr  mouse_window;
  161.    
  162.    switch(FindWindow(mk_long(eventp->where),&mouse_window))
  163.       {
  164.        case inContent:
  165.           if (mouse_window != FrontWindow())
  166.              SelectWindow(mouse_window);
  167.           break;
  168.           
  169.        case inDrag:
  170.           DragWindow(mouse_window, mk_long(eventp->where),&drag_rect);
  171.           break;
  172.           
  173.        case inGrow:
  174.           grow_window(mouse_window, mk_long(eventp->where), &drag_rect);
  175.           break;
  176.           
  177.        case inGoAway:
  178.           if (TrackGoAway(mouse_window,mk_long(eventp->where)))
  179.               finish();
  180.           break;
  181.           
  182.        default:
  183.           break;
  184.        }
  185.   }
  186. /**********************************************************************/  
  187. do_update(event)
  188.   EventRecord  *event;
  189. {
  190.  GrafPtr   save_graf;
  191.  WindowPtr update_window;
  192.  
  193.  if (FindWindow(mk_long(event->where),&update_window) != inSysWindow)
  194.     {if (update_window == hello_window)
  195.         {GetPort(&save_graf);
  196.          SetPort(update_window);
  197.          BeginUpdate(update_window);
  198.          ClipRect(&update_window->portRect);
  199.          EraseRect(&update_window->portRect);
  200.          DrawGrowIcon(update_window);
  201.          draw_content(update_window);
  202.          EndUpdate(update_window);
  203.          SetPort(save_graf);
  204.         }
  205.      }
  206. }
  207. /**********************************************************************/
  208. do_activate(event)
  209.   EventRecord  *event;
  210. {
  211.  WindowPtr event_window = (WindowPtr)event->message;
  212.  if (event_window == hello_window)
  213.     {DrawGrowIcon(event_window);
  214.      if (event->modifiers & 1)
  215.         SetPort(event_window);
  216.     }
  217. }
  218. /**********************************************************************/
  219. grow_window(window,mouse_point)
  220.   WindowPtr window;
  221.   Point     mouse_point;
  222. {
  223.  long new_bounds;
  224.  
  225.  inval_bars(window);
  226.  new_bounds = GrowWindow(window, mk_long(mouse_point),&grow_bounds);
  227.  if (0 == new_bounds)
  228.    return;
  229.  SizeWindow(window,LoWord(new_bounds),HiWord(new_bounds),TRUE);
  230.  inval_bars(window);
  231. }
  232. /**********************************************************************/
  233. inval_bars(window)
  234.   WindowPtr  window;
  235. {
  236.  Rect temp_rect, port_rect;
  237.  
  238.  port_rect = window->portRect;
  239.  SetRect(&temp_rect,port_rect.left,port_rect.bottom-16,port_rect.right,port_rect.bottom);
  240.  InvalRect(&temp_rect);
  241.  SetRect(&temp_rect,port_rect.right-16,port_rect.top,port_rect.right,port_rect.bottom);
  242.  InvalRect(&temp_rect);
  243. }
  244. /**********************************************************************/
  245. draw_content(window)
  246.   WindowPtr window;
  247. {
  248.  MoveTo(100,100);
  249.  DrawString("\pHello World!");
  250. }
  251. /**********************************************************************/
  252. finish()
  253. {
  254.  exit(0);
  255.  
  256. [LISTING THREE]
  257.  
  258. #include <windows.h>
  259. #include "hello.h"
  260.  
  261. BOOL NEAR       Initialize( HANDLE hInst, HANDLE hPrevInst, int nCmdShow );
  262. long FAR PASCAL WndProc   ( HWND hWnd, WORD wMessage, WORD wParam, LONG lParam);
  263. static char  szClass[40];
  264. static char  szTitle[40];
  265.  
  266.  int PASCAL WinMain( hInst, hPrevInst, lpszCmdLine, nCmdShow )
  267.    HANDLE      hInst;              /* Our instance handle */
  268.    HANDLE      hPrevInst;          /* Previous instance of this application */
  269.    LPSTR       lpszCmdLine;        /* Pointer to any command line params */
  270.    int         nCmdShow;           /* Parameter to use for first ShowWindow */
  271.    {
  272.     MSG        msg;                /* Message structure */
  273.  
  274.     if( ! Initialize( hInst, hPrevInst, nCmdShow ) )
  275.         return FALSE;
  276.  
  277.      while( GetMessage( &msg, NULL, 0, 0 ) ) {
  278.          TranslateMessage( &msg );
  279.          DispatchMessage( &msg );
  280.         }
  281.  
  282.     return msg.wParam;
  283.    }
  284. /************************************************************************
  285.       Initialize the application.
  286.       Returns TRUE if initialization succeeded, FALSE if failed.
  287.  ************************************************************************/
  288.  BOOL NEAR Initialize( hInst, hPrevInst, nCmdShow )
  289.    HANDLE      hInst;        /* Our Instance handle */
  290.    HANDLE      hPrevInst;    /* Previous instance handle, 0 if first */
  291.    int         nCmdShow;     /* Parameter from WinMain for ShowWindow */
  292.    {
  293.     WNDCLASS    WndClass;    /* Class structure for RegisterClass */
  294.     HWND        hWnd;        /* The window handle */
  295.     HMENU       hMenu;       /* Handle to the (system) menu */
  296.  
  297.     if( ! hPrevInst )
  298.        {
  299.          LoadString( hInst, IDS_CLASS,    szClass,    sizeof(szClass) );
  300.          LoadString( hInst, IDS_TITLE,    szTitle,    sizeof(szTitle) );
  301.          WndClass.style          = CS_HREDRAW | CS_VREDRAW;
  302.          WndClass.lpfnWndProc    = WndProc;
  303.          WndClass.cbClsExtra     = 0;
  304.          WndClass.cbWndExtra     = 0;
  305.          WndClass.hInstance      = hInst;
  306.          WndClass.hIcon          = LoadIcon( NULL, IDI_APPLICATION );
  307.          WndClass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  308.          WndClass.hbrBackground  = GetStockObject(WHITE_BRUSH);
  309.          WndClass.lpszMenuName   = NULL;
  310.          WndClass.lpszClassName  = szClass;
  311.  
  312.          if( ! RegisterClass( &WndClass ) )
  313.               return FALSE;
  314.        }
  315.     else
  316.        {
  317.          GetInstanceData(hPrevInst, szClass, sizeof(szClass));
  318.          GetInstanceData(hPrevInst, szTitle, sizeof(szTitle));
  319.        }
  320.     hWnd = CreateWindow(
  321.          szClass,          /* Class name */
  322.          szTitle,          /* Window title */
  323.          WS_OVERLAPPEDWINDOW,  /* window style */
  324.          CW_USEDEFAULT,    /* x */
  325.          0,                /* y */
  326.          CW_USEDEFAULT,    /* x width */
  327.          0,                /* y width */
  328.          NULL,             /* Parent hWnd (none for top-level) */
  329.          NULL,             /* Menu handle */
  330.          hInst,            /* Owning instance handle */
  331.          NULL              /* Parameter to pass in WM_CREATE (none) */
  332.      );
  333.     ShowWindow( hWnd, nCmdShow );
  334.     UpdateWindow( hWnd );
  335.  
  336.     return TRUE;
  337.    }
  338. /***********************************************************************
  339.  Process the messages
  340. ***********************************************************************/
  341.  long FAR PASCAL WndProc(hWnd, wMessage, wParam, lParam)
  342.   HWND hWnd;
  343.   WORD wMessage, wParam;
  344.   LONG lParam;
  345.  
  346.   {PAINTSTRUCT  ps;
  347.  
  348.    switch (wMessage)
  349.     {case WM_PAINT:
  350.        BeginPaint(hWnd,&ps);
  351.        TextOut(ps.hdc,10,100,"Hello World!",12);
  352.        EndPaint(hWnd,&ps);
  353.        break;
  354.      default:
  355.        return DefWindowProc( hWnd, wMessage, wParam, lParam );
  356.        break;
  357.     }
  358.    return 0L;
  359.   }
  360.  
  361.